package cn.com.oceansoft.business.mobile.danger.service;

import cn.com.oceansoft.business.mobile.danger.entity.CollecterInfo;
import cn.com.oceansoft.business.mobile.danger.entity.DangerContent;
import cn.com.oceansoft.business.mobile.danger.entity.Dict;
import cn.com.oceansoft.business.mobile.danger.entity.FlowInfo;
import cn.com.oceansoft.business.mobile.danger.persistence.ICollecterMapper;
import cn.com.oceansoft.business.mobile.danger.persistence.IDangertContent2Mapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2018/4/27.
 */
@Service
public class DangerContent2ServiceImpl implements IDnagerContentService {
   /* @Resource
    private ICommonDao commonDao;
    @Resource
    private WechatService wechatService;*/

  /*  private String fileExt;*/
    @Resource
    private IDangertContent2Mapper iDangertContentMapper;
    @Resource
    private ICollecterMapper collecterMapper;

    @Override
    public List<Dict> getDict(Dict dict) {
        try{
            return iDangertContentMapper.getDict(dict);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void saveDangerContent(DangerContent dangerContent) {
        try{
            iDangertContentMapper.insertDangerContent(dangerContent);
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            FlowInfo flowInfo=new FlowInfo();
            flowInfo.setDangerid(dangerContent.getId());
            collecterMapper.gxflowinfozt(flowInfo);
            iDangertContentMapper.insertFlowinfo(dangerContent);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<DangerContent> getListByTime(DangerContent dangerContent) {
       try{
           return iDangertContentMapper.getNowList(dangerContent);
       }catch (Exception e){
           e.printStackTrace();
       }
        return null;
    }

    @Override
    public JSONArray getJlxListByName(String name) {
        try{
            CollecterInfo collecterInfo=new CollecterInfo();
            collecterInfo.setName(name);
        List<Map<String,Object>> list=iDangertContentMapper.getJlx(collecterInfo);
            JSONArray result = new JSONArray();
            int count =0;
            for(int i =0;i<list.size();i++){
                //拼音首字母或者中文进行检索
                if(count>20){
                    break;
                }
                    String s = JSONObject.toJSONString(list.get(i));
                    result.add(JSONObject.parseObject(s));
                    count++;

                if("".equals(name)||null==name){
                    if(i>=9){
                        break;
                    }
                }
            }
            return result;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public JSONArray getfjList() {
        try{
            List<Map<String,Object>> list=iDangertContentMapper.getqy();
            JSONArray result = new JSONArray();
            for(int i =0;i<list.size();i++){
                String s = JSONObject.toJSONString(list.get(i));
                result.add(JSONObject.parseObject(s));
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public JSONArray getChildByParentid(String parentid, int deep) {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        JSONArray result = new JSONArray();
        CollecterInfo collecterInfo=new CollecterInfo();
        collecterInfo.setId(parentid);
        if(1==deep){
            list = iDangertContentMapper.getjd(collecterInfo);
        }
        if(2==deep){
            list = iDangertContentMapper.getsq(collecterInfo);
        }
        for(int i =0;i<list.size();i++){
            String s = JSONObject.toJSONString(list.get(i));
            result.add(JSONObject.parseObject(s));
        }
        return result;
    }

    @Override
    public JSONObject getObjectById(String id) {
        try{
            CollecterInfo collecterInfo=new CollecterInfo();
            collecterInfo.setId(id);
            List<Map<String,Object>> list = iDangertContentMapper.getonejd(collecterInfo);
            JSONObject result = new JSONObject();
            if(list.size()>0){
                String s = JSONObject.toJSONString(list.get(0));
                result = JSONObject.parseObject(s);
            }
            return  result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
